package com.swjd.OneToMany;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

//接收消息
public class ConsumerOne {
    public static void main(String[] args) throws MQClientException {
        //接收消息的对象 第一组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //接收的服务器地址
        consumer.setNamesrvAddr("localhost:9876");
        //设置接收的消息
        consumer.subscribe("topic2","*");
        //开启监听，接收消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
              //遍历消息集合
                for (MessageExt msg : list){
                //  System.out.println("收到消息"+msg);
                  //获取集合中的body信息
                  byte[] body = msg.getBody();
                  //传成string输出
                  System.out.println(new String(body));
              }
                   return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //启动服务
        consumer.start();
        System.out.println("接收服务已开启");
    }
}
